package demo7;


class Money implements Cloneable{
    public double m = 9.9;


    @Override
    public Money clone() {
        try {
            Money clone = (Money) super.clone();
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}
class Person implements Cloneable{
    public String name;
    public int age;

    public Money money = new Money();

    public Person(int age, String name) {
        this.age = age;
        this.name = name;

    }

    @Override
    public String toString() {
        return "Person{" +
                "name=" + name +
                ", age='" + age + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();
        tmp.money=(Money) this.money.clone();
        return tmp;
        //return super.clone();
    }

    @Override
    public boolean equals(Object obj) {
        Person temp = (Person) obj;
        return this.name.equals(temp.name) && this.age == temp.age;
    }
}


public class Test {

    public static void main(String[] args) throws CloneNotSupportedException{
        Person person = new Person(10,"zhangsan");
        Person person2 = new Person(10,"lisi");

        Person person1 = (Person) person.clone();

            System.out.println("person "+person.money.m);
            System.out.println("person1 "+person1.money.m);
            System.out.println("------------");
            person.money.m=99.9;
            System.out.println("person "+person.money.m);
            System.out.println("person1 "+person1.money.m);

        System.out.println(person.equals(person2));
    }
}
